<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/ztree/zTreeStyle.css">

</head>
<body>
<button id="save_btn" class="btn btn-info" type="button">分配许可权限</button>
   
   <ul id="treeDemo" class="ztree"></ul>
   <input type="hidden" value="${requestScope.rid}" id="rid"/>
<script src="${pageContext.request.contextPath}/static/js/jquery-1.12.3.min.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/static/ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript">
$(function(){
 
	var setting = {
            check : {
                enable : true 
            },
			view: {
				selectedMulti: false,
				 addDiyDom: function(treeId, treeNode){
						//获取每个节点的小图标
						var icoObj = $("#" + treeNode.tId + "_ico"); // tId = permissionTree_1, $("#permissionTree_1_ico")
						//如果存在替换小图标，就替换小图标
					
						if ( treeNode.icon ) {
							icoObj.removeClass("button ico_docu ico_open").addClass(treeNode.icon).css("background","");
						}
						
		                
					}
			},
           
		};
		
		
		//var d = [{"id":1,"pid":0,"name":"系统权限菜单","url":null,"icon":"glyphicon glyphicon-th-large","open":true,"checked":false,"children":[{"id":2,"pid":1,"seqno":0,"name":"控制面板","url":"dashboard.htm","icon":"fa fa-desktop","open":true,"checked":false,"children":[]},{"id":6,"pid":1,"seqno":1,"name":"消息管理","url":"message/index.htm","icon":"fa fa-weixin","open":true,"checked":false,"children":[]},{"id":7,"pid":1,"seqno":1,"name":"权限管理","url":"","icon":"fa fa-cogs","open":true,"checked":false,"children":[{"id":8,"pid":7,"seqno":1,"name":"用户管理","url":"user/index.htm","icon":"fa fa-user","open":true,"checked":false,"children":[]},{"id":9,"pid":7,"seqno":1,"name":"角色管理","url":"role/index.htm","icon":"fa fa-graduation-cap","open":true,"checked":false,"children":[]},{"id":10,"pid":7,"seqno":1,"name":"许可管理","url":"permission/index.htm","icon":"fa fa-check-square-o","open":true,"checked":false,"children":[]}]},{"id":11,"pid":1,"seqno":1,"name":"资质管理","url":"","icon":"fa fa-certificate","open":true,"checked":false,"children":[{"id":12,"pid":11,"seqno":1,"name":"分类管理","url":"cert/type.htm","icon":"fa fa-th-list","open":true,"checked":false,"children":[]},{"id":13,"pid":11,"seqno":1,"name":"资质管理","url":"cert/index.htm","icon":"fa fa-certificate","open":true,"checked":false,"children":[]}]},{"id":15,"pid":1,"seqno":1,"name":"流程管理","url":"process/index.htm","icon":"fa fa-random","open":true,"checked":false,"children":[]},{"id":16,"pid":1,"seqno":1,"name":"审核管理","url":"","icon":"fa fa-check-square","open":true,"checked":false,"children":[{"id":17,"pid":16,"seqno":1,"name":"实名认证人工审核","url":"process/cert.htm","icon":"fa fa-check-circle-o","open":true,"checked":false,"children":[]}]}]}];
		//$.fn.zTree.init($("#treeDemo"), setting,d); //异步访问数据
		//本次发送ajax请求的目的获得根树关联的json
		$.getJSON("${pageContext.request.contextPath}/roles/getPersTree?rid=${requestScope.rid}",function(data){
			
			  
		    $.fn.zTree.init($("#treeDemo"), setting, data.tree);
		    //得到树对象，参数直接写id，不写#
		    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
		    //获得所有的节点
		   
		     for(var i=0;i<data.pers.length;i++){
		    	 //根据树节点的id得到树节点
		   	    var node=treeObj.getNodeByParam("id", data.pers[i].permission_id, null);
		         //将treeObj上的node设置为true
		    	treeObj.checkNode( node, true );
		    }
		});
	
	
	
		//点击保存按钮 
		$("#save_btn").click(function(){
			//得到树对象
			 var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			var array=[];
			//得到所有选中的节点
			var nodes=treeObj.getCheckedNodes(true);
			for(var i=0;i<nodes.length;i++){
				//将选中的节点的id放到数组中
				array.push(nodes[i].id);
			
				
			}
			//向服务器发送ajax请求
			$.post("${pageContext.request.contextPath}/roles/updatePersByRole",{"rid":$("#rid").val(),"pids":array.join(",")},function(data){
				
				alert(data);
				
			})
			
		}); 
	
})
</script>
</body>
</html>